package procsim;

import java.awt.*;

public class GRegister extends Register implements GraphicalElement {

    private int width = 110;
    private int height = 60;
    private int x, y;
    
    public GRegister(int numOfBits, int init, MSignal in) {
        super(numOfBits, init, in);
    }

    public GRegister(int numOfBits, int init, MSignal in, String name) {
        super(numOfBits, init, in, name);
    }

    @Override
    public void setCoords(int x, int y){
        this.x = x;
        this.y = y;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void drawIt(Graphics2D g, int x, int y) {
        Util.saveGraphics(g);
        int strlen = getName().length();
        int showVal = Util.int2Hex(result().get(), getNumOfBits()).length();
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        g.drawRect(x+1, y+1, width-2, height-2);
        g.drawString("LD", x+4, y+height/2+5);
        g.drawString("CL", x+width-17, y+height/2+5);
        g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 14));
        g.drawString(getName(), x+(getWidth()/2)-3-(strlen*3), y+getHeight()/2);
        g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 10));
        g.setColor(Color.GRAY);
        g.drawString(Util.int2Hex(result().get(), getNumOfBits()), x+(getWidth()/2)-(showVal*3), y+getHeight()/2+10);
        Util.restoreGraphics(g);
    }

    @Override
    public void drawIt(Graphics2D g) {
        drawIt(g, x, y);
    }

    public Coords getLdCoords(int length){
        return new Coords(x-length, y+29, x, y+29);
    }

    public Coords getClCoords(int length){
        return new Coords(x+width+length, y+29, x+width, y+29);
    }

    public Coords getInCoords(int length){
        return new Coords(x+width/2, y, x+width/2, y-length);
    }

    public Coords getOutCoords(int length){
        return new Coords(x+width/2, y+height, x+width/2, y+height+length);
    }
}
